package org.wanho.txy.ems.service.proxy;

import org.wanho.exception.ServiceException;
import org.wanho.factory.ObjectFactory;
import org.wanho.transaction.TransactionManager;
import org.wanho.txy.ems.entity.User;
import org.wanho.txy.ems.exception.UserModifyException;
import org.wanho.txy.ems.exception.UsernameExistsException;
import org.wanho.txy.ems.param.UserParam;
import org.wanho.txy.ems.service.UserService;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2023-05-10 15:43
 * Description：<描述>
 */
public class UserServiceProxy implements UserService {

    private TransactionManager tx = (TransactionManager) ObjectFactory.getObject("tx");
    private  UserService userService = (UserService) ObjectFactory.getObject("userService");

    @Override
    public User login(String username, String password) {
        try {
            tx.beginTransaction();
            User user = userService.login(username, password);
            tx.commit();
            return user;
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public void register(User user) {
        try {
            tx.beginTransaction();
            userService.register(user);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public List<User> findByCondition(UserParam userParam, int pageNo, int pageSize) {
        try {
            tx.beginTransaction();
            List<User> users = userService.findByCondition(userParam, pageNo, pageSize);
            tx.commit();
            return users;
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public int selectCount(UserParam userParam) {
        try {
            tx.beginTransaction();
            int count = userService.selectCount(userParam);
            tx.commit();
            return count;
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public void modifyStatus(int id, int status) {
        try {
            tx.beginTransaction();
            userService.modifyStatus(id,status);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public void removeById(int id) {
        try {
            tx.beginTransaction();
            userService.removeById(id);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public User findById(int id) {
        try {
            tx.beginTransaction();
            User user = userService.findById(id);
            tx.commit();
            return user;
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public void modify(User user) throws UserModifyException {
        try {
            tx.beginTransaction();
            userService.modify(user);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public void add(User user) throws UsernameExistsException {
        try {
            tx.beginTransaction();
            userService.add(user);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }
}
